闘本 1.6 文字列圧縮
提出
code: text
s = "aabcccccaaa"
def compress(s):
ans = ""
count = 0
for i in range(len(s)-1):
count += 1
else:
ans += str(count+1) if count else "1"
count = 0
ans-1 = str(int(ans-1) + 1) else:
ans += "1"
if len(ans) >= s:
return ans
else:
return s
print(compress(s))
実行結果
code: zsh
File "/Users/wafuwafu13/Desktop/CtCI-6th-Edition-Python/debug.py", line 18, in compress
if len(ans) >= s:
^^^^^^^^^^^^^
TypeError: '>=' not supported between instances of 'int' and 'str'